package com.tech.zhidengxian.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tech.zhidengxian.mapper.GoodsMapper;
import com.tech.zhidengxian.model.constant.GoodsState;
import com.tech.zhidengxian.model.dataobject.GoodsDO;
import com.tech.zhidengxian.model.dto.GoodsDTO;
import com.tech.zhidengxian.model.dto.page.MyPage;
import com.tech.zhidengxian.model.dto.request.GoodsRequest;
import com.tech.zhidengxian.service.api.GoodsService;
import com.tech.zhidengxian.util.JacksonUtils;
import com.tech.zhidengxian.util.ResultAssert;
import com.tech.zhidengxian.util.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class GoodServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public MyPage<GoodsDTO> pageQueryRecommend(GoodsRequest goodsRequest) {
        IPage<GoodsDO> goodsDOIPage = goodsMapper.pageQueryRecommend(goodsRequest);
        List<GoodsDO> records = goodsDOIPage.getRecords();
        List<GoodsDTO> goodsDTOList = JacksonUtils.convertList(records, GoodsDTO.class);
        //打乱顺序
        Collections.shuffle(goodsDTOList);
        return MyPage.of(goodsDTOList,goodsRequest.getCurrentPage(),goodsRequest.getPageSize(),goodsDOIPage.getTotal());

    }

    @Override
    public MyPage<GoodsDTO> pageQueryLocalCity(GoodsRequest goodsRequest) {
        IPage<GoodsDO> goodsDOIPage = goodsMapper.pageQueryLocalCity(goodsRequest);
        List<GoodsDO> records = goodsDOIPage.getRecords();
        List<GoodsDTO> goodsDTOList = JacksonUtils.convertList(records, GoodsDTO.class);
        //打乱顺序
        Collections.shuffle(goodsDTOList);
        return MyPage.of(goodsDTOList,goodsRequest.getCurrentPage(),goodsRequest.getPageSize(),goodsDOIPage.getTotal());

    }

    @Override
    public MyPage<GoodsDTO> pageQuerySchool(GoodsRequest goodsRequest) {
        IPage<GoodsDO> goodsDOIPage = goodsMapper.pageQuerySchool(goodsRequest);
        List<GoodsDO> records = goodsDOIPage.getRecords();
        List<GoodsDTO> goodsDTOList = JacksonUtils.convertList(records, GoodsDTO.class);
        //打乱顺序
        Collections.shuffle(goodsDTOList);
        return MyPage.of(goodsDTOList,goodsRequest.getCurrentPage(),goodsRequest.getPageSize(),goodsDOIPage.getTotal());
    }

    @Override
    public MyPage<GoodsDTO> pageQueryByDetail(GoodsRequest goodsRequest) {
        IPage<GoodsDO> iPage = goodsMapper.pageQueryByDetail(goodsRequest);
        List<GoodsDO> records = iPage.getRecords();
        List<GoodsDTO> goodsDTOS = JacksonUtils.convertList(records, GoodsDTO.class);
        return MyPage.of(goodsDTOS,goodsRequest.getCurrentPage(),goodsRequest.getPageSize(),iPage.getTotal());
    }

    @Override
    public List<GoodsDTO> queryListByFid(Long fansId) {
        List<GoodsDO> goodsDOS = goodsMapper.queryListByFid(fansId);
        return JacksonUtils.convertList(goodsDOS, GoodsDTO.class);
    }

    @Override
    public GoodsDTO publishGoods(GoodsDTO goodsDTO) {
        int insert;
        //商品重新发布
        if (!Objects.isNull(goodsDTO.getId())){
            insert = updateGoods(goodsDTO);
        }else {
            goodsDTO.setPublishTime(TimeUtil.currentDate());
            goodsDTO.setUpdateTime(TimeUtil.currentDate());
            GoodsDO goodsDO = JacksonUtils.convert(goodsDTO, GoodsDO.class);
            insert = goodsMapper.insert(goodsDO);
            goodsDTO.setId(goodsDO.getId());
        }
        ResultAssert.assertGreaterThanZero(insert,"商品发布异常");
        return goodsDTO;
    }

    @Override
    public GoodsDTO queryGoodsById(Long id) {
        GoodsDO goodsDO = goodsMapper.selectById(id);
        GoodsDTO goodsDTO = JacksonUtils.convert(goodsDO, GoodsDTO.class);
//        if (goodsDTO!=null){
//            GoodsDTO dto = new GoodsDTO();
//            dto.setId(goodsId);
//            dto.setBrowseCount(1L);
//        }
        return goodsDTO;
    }

    @Override
    public int updateGoodsCount(GoodsDTO goodsDTO) {
        GoodsDO goodsDO = JacksonUtils.convert(goodsDTO, GoodsDO.class);
        int count = goodsMapper.updateGoodsCount(goodsDO);
        ResultAssert.assertGreaterThanZero(count,"商品相关数量修改失败");
        return count;
    }
    @Override
    public int updateGoodsState(Long id, GoodsState goodsState){
        GoodsDO goodsDO = new GoodsDO();
        goodsDO.setId(id);
        goodsDO.setGoodsState(goodsState);
        int update = goodsMapper.updateById(goodsDO);
        ResultAssert.assertGreaterThanZero(update,"商品状态修改异常");
        return update;
    }

    @Override
    public int updateGoods(GoodsDTO goodsDTO){
        goodsDTO.setUpdateTime(TimeUtil.currentDate());
        GoodsDO goodsDO = JacksonUtils.convert(goodsDTO, GoodsDO.class);
        int update = goodsMapper.updateById(goodsDO);
        ResultAssert.assertGreaterThanZero(update,"商品状态修改异常");
        return update;
    }

    @Override
    public void deleteById(Long id) {
        GoodsDO goodsDO = new GoodsDO();
        goodsDO.setId(id);
        goodsDO.setIsDeleted(true);
        int cnt = goodsMapper.updateById(goodsDO);
        ResultAssert.assertGreaterThanZero(cnt,"删除商品失败");
        return ;
    }



//    @Override
//    public int updateGoods(GoodsDTO goodsDTO) {
//        GoodsDO goodsDO = JacksonUtils.convert(goodsDTO, GoodsDO.class);
//        return goodsMapper.updateGoods(goodsDO);
//    }

}
